package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.OutStockBillDetailMapper;
import com.canaan.business.domain.inOutStock.OutStockBillDetail;
import com.canaan.business.service.inOutStock.IOutStockBillDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 出库单明细Service业务层处理
 *
 * @author canaan
 * @date 2025-03-29
 */
@Service
public class OutStockBillDetailServiceImpl implements IOutStockBillDetailService {
    @Resource
    private OutStockBillDetailMapper outStockBillDetailMapper;

    /**
     * 查询出库单明细
     *
     * @param id 出库单明细主键
     * @return 出库单明细
     */
    @Override
    public OutStockBillDetail selectOutStockBillDetailById(Long id) {
        return outStockBillDetailMapper.selectOutStockBillDetailById(id);
    }

    /**
     * 查询出库单明细列表
     *
     * @param outStockBillDetail 出库单明细
     * @return 出库单明细
     */
    @Override
    public List<OutStockBillDetail> selectOutStockBillDetailList(OutStockBillDetail outStockBillDetail) {
        return outStockBillDetailMapper.selectOutStockBillDetailList(outStockBillDetail);
    }

    /**
     * 新增出库单明细
     *
     * @param outStockBillDetail 出库单明细
     * @return 结果
     */
    @Override
    public int insertOutStockBillDetail(OutStockBillDetail outStockBillDetail) {
        if(StringUtils.isBlank(outStockBillDetail.getNo())){
            outStockBillDetail.setNo(TableNoUtil.getNo(OutStockBillDetail.class));
        }
        outStockBillDetail.setCreateBy(SecurityUtils.getUsername());
        outStockBillDetail.setCreateTime(DateUtils.getNowDate());
        return outStockBillDetailMapper.insertOutStockBillDetail(outStockBillDetail);
    }

    /**
     * 批量新增出库单明细
     *
     * @param outStockBillDetailList 出库单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertOutStockBillDetail(List<OutStockBillDetail> outStockBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (OutStockBillDetail outStockBillDetail : outStockBillDetailList){
            if(StringUtils.isBlank(outStockBillDetail.getNo())){
                outStockBillDetail.setNo(TableNoUtil.getNo(OutStockBillDetail.class));
            }
            outStockBillDetail.setCreateBy(username);
            outStockBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<outStockBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > outStockBillDetailList.size()) {
                endIndex = outStockBillDetailList.size();
            }
            insertNum = insertNum + outStockBillDetailMapper.batchInsertOutStockBillDetail(outStockBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改出库单明细
     *
     * @param outStockBillDetail 出库单明细
     * @return 结果
     */
    @Override
    public int updateOutStockBillDetail(OutStockBillDetail outStockBillDetail) {
        outStockBillDetail.setUpdateBy(SecurityUtils.getUsername());
        outStockBillDetail.setUpdateTime(DateUtils.getNowDate());
        return outStockBillDetailMapper.updateOutStockBillDetail(outStockBillDetail);
    }

    /**
     * 批量修改出库单明细
     *
     * @param outStockBillDetailList 出库单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateOutStockBillDetail(List<OutStockBillDetail> outStockBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (OutStockBillDetail outStockBillDetail : outStockBillDetailList){
            outStockBillDetail.setUpdateBy(username);
            outStockBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<outStockBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > outStockBillDetailList.size()) {
                endIndex = outStockBillDetailList.size();
            }
            updateNum = updateNum + outStockBillDetailMapper.batchUpdateOutStockBillDetail(outStockBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除出库单明细
     *
     * @param ids 需要删除的出库单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteOutStockBillDetailByIds(Long[] ids) {
        return outStockBillDetailMapper.deleteOutStockBillDetailByIds(ids);
    }

    /**
     * 删除出库单明细信息
     *
     * @param id 出库单明细主键
     * @return 结果
     */
    @Override
    public int deleteOutStockBillDetailById(Long id) {
        return outStockBillDetailMapper.deleteOutStockBillDetailById(id);
    }

    /**
     * 查询出库单明细
     *
     * @param no 编号
     * @return 出库单明细
     */
    @Override
    public OutStockBillDetail selectOutStockBillDetailByNo(String no) {
        return outStockBillDetailMapper.selectOutStockBillDetailByNo(no);
    }

    /**
     * 批量删除出库单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteOutStockBillDetailByNos(String[] nos) {
        return outStockBillDetailMapper.deleteOutStockBillDetailByNos(nos);
    }

    /**
     * 删除出库单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteOutStockBillDetailByNo(String no) {
        return outStockBillDetailMapper.deleteOutStockBillDetailByNo(no);
    }
}
